/*
 * Copyright (C) 2011 CrystalRange
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.crystalrange.enterprise.web;

import com.googlecode.genericdao.search.Search;
import java.util.List;
import org.crystalrange.enterprise.model.CompanyCode;
import org.crystalrange.enterprise.service.CompanyCodeService;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.util.GenericForwardComposer;
import org.zkoss.zkplus.databind.AnnotateDataBinder;
import org.zkoss.zul.Messagebox;
import org.zkoss.zul.Window;

public class CompanyCodeSelectController extends GenericForwardComposer {

    protected CompanyCodeService companyCodeService;
    protected CompanyCode selectedCompanyCode;
    protected AnnotateDataBinder binder;
    protected Search searchCompanyCode;
    protected List<CompanyCode> companyCodes;

    public CompanyCodeService getCompanyCodeService() {
        return this.companyCodeService;
    }

    public void setCompanyCodeService(CompanyCodeService companyCodeService) {
        this.companyCodeService = companyCodeService;
    }

    public CompanyCode getSelectedCompanyCode() {
        return this.selectedCompanyCode;
    }

    public void setSelectedCompanyCode(CompanyCode selectedCompanyCode) {
        this.selectedCompanyCode = selectedCompanyCode;
    }

    public List<CompanyCode> getCompanyCodes() {
        return this.companyCodes;
    }

    @Override
    public void doAfterCompose(Component component) throws Exception {
        super.doAfterCompose(component);

        this.searchCompanyCode = new Search(CompanyCode.class);
        this.searchCompanyCode.setMaxResults(100);
        this.searchCompanyCode.addFetch("createdBy");
        this.searchCompanyCode.addFetch("updatedBy");
        this.companyCodes = this.companyCodeService.search(this.searchCompanyCode);

        component.setAttribute("controller", this);
    }

    public void onClick$buttonOk(Event event) throws Exception {
        if (this.selectedCompanyCode != null) {
            this.execution.setAttribute("selectedCompanyCode", this.selectedCompanyCode);
            this.execution.setAttribute("result", "ok");
            this.self.detach();
        } else {
            Messagebox.show("Please select a Company Code.");
        }
    }

    public void onClick$buttonCancel(Event event) throws Exception {
        this.execution.setAttribute("selectedCompanyCode", null);
        this.execution.setAttribute("result", "cancel");
        this.self.detach();
    }

    public void onClick$buttonSearch(Event event) throws Exception {
        this.execution.setAttribute("searchCompanyCode", this.searchCompanyCode);

        Window window = (Window) this.execution.createComponents("~./zul/html/companyCodeSearch.zul", null, null);
        window.setParent(self);
        window.doModal();

        if (this.execution.getAttribute("result") == "ok") {
            this.searchCompanyCode = (Search) this.execution.getAttribute("searchCompanyCode");
        }

        this.companyCodes = this.companyCodeService.search(this.searchCompanyCode);
        this.binder.loadAll();
    }
}
